//
//  picCollectionView.swift
//  kingWeiBo
//
//  Created by King on 15/9/16.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit
import SDWebImage

class picCollectionView: UICollectionView {
    
    var status:Status?{
        didSet{
            sizeToFit()
            reloadData()
            if status?.imageURL?.count > 0{
                print("height == >>> ",self.frame.size.height,"-",status?.imageURL?.count)
            }
        }
    
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
        return setPicViewSize()
    }
    
    //MARK: - 设置视图大小
    private func setPicViewSize() -> CGSize{
        /// 单张图大小
        let oneSize = CGSize(width: 90, height: 90)
        /// 每行图片的最大数量
        let picNumOfRow:CGFloat = 3
        /// 间距
        let margin:CGFloat = 10
        
        layouts.itemSize = oneSize
        ///  图片数量
        let count = status?.pic_urls?.count ?? 0
        // 根据图片数量计算视图大小
        if count == 0{
            
            return CGSizeZero
        }
        if count == 1{
//            let key = status!.imageURL
//          let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            
            let size = CGSize(width: 90 , height: 90)

            layouts.itemSize = size
            return size
        }
        if count == 4{
            let width = oneSize.width * 2 + margin
            let heigth = oneSize.height * 2 + margin
            let size = CGSize(width: width, height: heigth )

            return size
        
        }
        
        // 计算行
        let row = CGFloat(count - 1) / picNumOfRow + 1
        let width = oneSize.width * picNumOfRow  + margin * (picNumOfRow - 1)
        let height = row * oneSize.height + (row - 1) * margin - 56 - 9
        let size = CGSize(width: width, height: height)

        return size
    
    }
    let layouts = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layouts)
        
        backgroundColor = UIColor.lightGrayColor()
        self.dataSource = self
        registerClass(picCollectionCell.self , forCellWithReuseIdentifier: "collectioncell")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

}

extension picCollectionView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //TODO:as! 可能出错
       let cell =  collectionView.dequeueReusableCellWithReuseIdentifier("collectioncell", forIndexPath: indexPath) as! picCollectionCell
        cell.backgroundColor = UIColor.greenColor()
        cell.imageURL = status!.imageURL![indexPath.item]
        print(cell.imageURL)
        return cell
        
    }
    
    
    
    class picCollectionCell:UICollectionViewCell {
        
        var imageURL:NSURL?{
            didSet{
                
                iconView.sd_setImageWithURL(imageURL)
            
            }
            
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUpUI()
        }

        func setUpUI(){
            contentView.addSubview(iconView)
            iconView.ff_Fill(contentView)
        
        }
        
        //MARK: - 懒加载控件
        private lazy var iconView:UIImageView = UIImageView()
        
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        
    }
    
}